﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.basicData;
using baseSet.importExport;
using dhDataAcess.dataAcess.purchaseTableAdapters ;
using dhDataAcess.dataAcess;
using baseSet;
using dhDataAcess.produceLinq;
using dhDataAcess.baseBLL;
using dhSystemBLL.orderData;

namespace dhsystem.produceForm.pdrk
{
    public partial class FrmpdckMasterAdd : Form
    {
        purchase.pdckDetailDataTable pdckDetailTable = new purchase.pdckDetailDataTable();
        pdckDetailModel pdckDetailRow = new pdckDetailModel();
        pdckBLL pdck = new pdckBLL();
        deptBLL dept = new deptBLL();
        stockListBLL stockList = new stockListBLL();
        workerListBLL workerList = new workerListBLL();
        //  pdrkMasterModel pdrkMasterRow = new pdrkMasterModel();
        purchase.pdckMasterDataTable masterTable = new purchase.pdckMasterDataTable();

        BindingSource b2;
        string state;
        public FrmpdckMasterAdd(BindingSource b1, string _state)
        {
            InitializeComponent();
            state = _state;
            b2 = b1;
        }

        private void FrmpdckMasterAdd_Load(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                textbillNo.Text = "P" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("胚带出库单号");
                bindingSourcepdrkDetail.DataSource = pdck.getspdckDetailAllBybillNo (textbillNo.Text);
                dGridDetail.DataSource = bindingSourcepdrkDetail;
                //b2.AddNew();

            }
            bindingSourcedeptList.DataSource = dept.getdeptListAll();
            comboBoxdeptNum.DataSource = bindingSourcedeptList;
            comboBoxdeptNum.DisplayMember = "deptName";
            comboBoxdeptNum.ValueMember = "deptNum";
            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";
            //bindingSourcestockList.DataSource = stockList.getstockListAll();
            //comboBoxstockname.DataSource = bindingSourcestockList;
            //comboBoxstockname.DisplayMember = "stockName";
            //comboBoxstockname.ValueMember = "stockName";
          
            if (state == "编辑")
            {
               // comboBoxdeptNum.DataBindings.Add("SelectedValue", b2, "deptNum");
                comboBoxworkername.DataBindings.Add("SelectedValue", b2, "workername");
                textbillNo.DataBindings.Add("text", b2, "billNo");
                dateTimebillDate.DataBindings.Add("value", b2, "billDate");
                textstockname.DataBindings.Add("text", b2, "stockname");
                textmark.DataBindings.Add("text", b2, "mark");
                bindingSourcepdrkDetail.DataSource = pdck.getspdckDetailAllBybillNo (textbillNo.Text);
                dGridDetail.DataSource = bindingSourcepdrkDetail;
            }
            DcolumnSet.colSet(moduleInfo.pdckDetail, dGridDetail);
            dGridDetail.AllowUserToAddRows = false;
            comboBoxdeptNum.Text = "仓储部";
            comboBoxworkername.Text = "兰胜才";
        }
        int i = 0;
        //保存按钮，插入或修改数据
        private void toolSave_Click(object sender, EventArgs e)
        {
            if (textbillStatus.Text == "结算")
            {
                MessageBox.Show("此订单已经结算，不能进行修改。如需修改请反结算");
                return;
            }
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输出库单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存胚带入库表信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        if (i == 0 && state == "新增")
                        {
                            state = "新增";
                        }
                        else
                        {
                            state = "编辑";
                        }
                        i++;
                        dhDataAcess.dataAcess.purchase.pdckDetailDataTable ptable = (dhDataAcess.dataAcess.purchase.pdckDetailDataTable)bindingSourcepdrkDetail.DataSource;
                        purchase.pdckMasterRow pdckMasterRow = masterTable.NewpdckMasterRow();
                        pdckMasterRow.billNo = textbillNo.Text;
                        //pdckMasterRow.deptNum = comboBoxdeptNum.SelectedValue.ToString();
                        pdckMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                        pdckMasterRow.billDate = dateTimebillDate.Value;
                        pdckMasterRow.billStatus = "";
                        pdckMasterRow.stockname = textstockname.Text;
                        pdckMasterRow.createDate = DateTime.Now;
                        pdckMasterRow.createUser = GlobalInfo.UserName;
                        pdckMasterRow.updateDate = DateTime.Now;
                        pdckMasterRow.updateUser = GlobalInfo.UserName;
                        pdckMasterRow.mark = textmark.Text;
                        masterTable.AddpdckMasterRow(pdckMasterRow);
                        masterTable.AcceptChanges();

                        foreach (purchase.pdckDetailRow a in ptable)
                        {
                            var s = ptable.Where(x => x.pName == a.pName).ToList();
                            if (s.Count() > 1)
                            {
                                MessageBox.Show("存在两条相同产品名称的记录，不能保存");
                                return;
                            }
                            if (!a.IsqtyNull())
                            {
                                a.qty = a.qty;
                            }
                            else
                            {
                                DialogResult sresult = MessageBox.Show("数量为空是否保存！", "提示信息", MessageBoxButtons.YesNo);
                                if (sresult == DialogResult.Yes)
                                {
                                    a.qty = 0;
                                }
                                if (sresult == DialogResult.No)
                                {
                                    MessageBox.Show("保存已取消");
                                    return;
                                }
                            }
                        }
                        pdck.pdckInsert(masterTable, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourcepdrkMaster.DataSource = pdck.getpdckMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //菜单栏删除按钮
        private void toolDel_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //菜单栏新增按钮
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            state = "新增";
            masterTable.Clear();

            textbillNo.Text = "P" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("胚带入库单号");
            bindingSourcepdrkDetail.DataSource = pdck.getspdckDetailAllBybillNo (textbillNo.Text);
            dGridDetail.DataSource = bindingSourcepdrkDetail;

            bindingSourcedeptList.DataSource = dept.getdeptListAll();
            comboBoxdeptNum.DataSource = bindingSourcedeptList;
            comboBoxdeptNum.DisplayMember = "deptName";
            comboBoxdeptNum.ValueMember = "deptNum";
            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";

            DcolumnSet.colSet(moduleInfo.pdckDetail, dGridDetail);
            dGridDetail.AllowUserToAddRows = false;
            comboBoxdeptNum.Text = "仓储部";
            comboBoxworkername.Text = "兰胜才";
            dateTimebillDate.Value = DateTime.Now;
        }
        //菜单栏退出按钮
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
     
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textbillStatus.Text == "结算")
            {
                MessageBox.Show("此订单已经结算，不能进行修改。如需修改请反结算");
                return;
            }
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输出库单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存胚带入库表信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        if (i == 0 && state == "新增")
                        {
                            state = "新增";
                        }
                        else
                        {
                            state = "编辑";
                        }
                        i++;
                        dhDataAcess.dataAcess.purchase.pdckDetailDataTable ptable = (dhDataAcess.dataAcess.purchase.pdckDetailDataTable)bindingSourcepdrkDetail.DataSource;
                        purchase.pdckMasterRow pdckMasterRow = masterTable.NewpdckMasterRow();
                        pdckMasterRow.billNo = textbillNo.Text;
                        //pdckMasterRow.deptNum = comboBoxdeptNum.SelectedValue.ToString();
                        pdckMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                        pdckMasterRow.billDate = dateTimebillDate.Value;
                        pdckMasterRow.billStatus = "";
                        pdckMasterRow.stockname = textstockname.Text;
                        pdckMasterRow.createDate = DateTime.Now;
                        pdckMasterRow.createUser = GlobalInfo.UserName;
                        pdckMasterRow.updateDate = DateTime.Now;
                        pdckMasterRow.updateUser = GlobalInfo.UserName;
                        pdckMasterRow.mark = textmark.Text;
                        masterTable.AddpdckMasterRow(pdckMasterRow);
                        masterTable.AcceptChanges();
                       
                        foreach (purchase.pdckDetailRow a in ptable)
                        {
                            var s = ptable.Where(x => x.pName == a.pName).ToList();
                            if (s.Count() > 1)
                            {
                                MessageBox.Show("存在两条相同产品名称的记录，不能保存");
                                return;
                            }
                            if (!a.IsqtyNull ())
                            {
                                a.qty = a.qty;
                            }
                            else { 
                    DialogResult sresult = MessageBox.Show("数量为空是否保存！", "提示信息", MessageBoxButtons.YesNo);
                    if (sresult == DialogResult.Yes)
                    {
                        a.qty = 0;
                    }
                    if (sresult == DialogResult.No )
                    {
                        MessageBox.Show("保存已取消");
                        return;
                    }
                       }
                        }
                        pdck.pdckInsert(masterTable, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourcepdrkMaster.DataSource = pdck.getpdckMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //添加子件
        private void buttAdd_Click(object sender, EventArgs e)
        {
            FrmpdrkDetailAdd pdrkDetail = new FrmpdrkDetailAdd(bindingSourcepdrkDetail, "新增", textbillNo.Text);
            pdrkDetail.ShowDialog();
        }
        //编辑子件
        private void buttedit_Click(object sender, EventArgs e)
        {
            FrmpdrkDetailAdd pdrkDetail = new FrmpdrkDetailAdd(bindingSourcepdrkDetail, "编辑", textbillNo.Text);
            pdrkDetail.ShowDialog();
        }
        //删除子件
        private void buttDel_Click(object sender, EventArgs e)
        {

            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        
        //private void buttuserselect_Click(object sender, EventArgs e)
        //{
        //    string field;
        //    field = "workerNum";
        //    Frmprodutselect productselect = new Frmprodutselect(moduleInfo.workerList, field);
        //    DialogResult result = productselect.ShowDialog();
        //    if (result == DialogResult.Yes)
        //    {
        //        comboBoxworkername.SelectedValue = productselect.pname;
        //    }
        //}
        //选择入库人
        private void button1_Click(object sender, EventArgs e)
        {
            string field;
            field = "workerNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.workerList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxworkername.SelectedValue = productselect.pname;
            }
        }

        private void buttuserselect_Click(object sender, EventArgs e)
        {

        }
    }
}
